package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    /**根据id删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id不能为空");
        }
        AdSensitive sensitive = this.getById(id);
        if(sensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST,"查不到这个敏感词");
        }
        this.removeById(id);

        return ResponseResult.okResult();
    }

    /**分页查询敏感词汇
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult querySensitiveList(SensitiveDTO dto) {
        //参数检查
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"分页参数不能为空");
        }
        //分页参数检查
        dto.checkParam();
        //创建mp查询对象(lambdaQuery)
        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.lambdaQuery();
        //创建分页对象传入参数
        Page<AdSensitive> page = new Page<>(dto.getPage(), dto.getSize());

        if(StringUtils.isNoneBlank(dto.getName())){
            queryWrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        //封装分页对象
        IPage<AdSensitive> result = page(page, queryWrapper);

        //结果封装
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), result.getTotal(), result.getRecords());

        return pageResponseResult;
    }

    /**添加敏感词汇
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult insertSensitive(AdSensitive adSensitive) {
        //数据检查
        if(StringUtils.isBlank(adSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数传入有误");
        }
        //判断添加的敏感词是否重复
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        //若查询到敏感词则证明敏感词已存在
        if(count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词已存在");
        }
        //保存时间
        adSensitive.setCreatedTime(new Date());
        //添加
        this.save(adSensitive);
        //返回结果
        return ResponseResult.okResult();
    }

    /**修改敏感词汇
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(AdSensitive adSensitive) {
        //数据检查
        if(adSensitive.getId()==null||adSensitive.getSensitives()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数传入有误");
        }
        //判断id是否存在
        int count1 = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getId, adSensitive.getId()));
        if(count1==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id不存在");
        }

        //检查修改后的敏感词是否已经存在
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if(count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"您修改的敏感词已存在");
        }
        adSensitive.setCreatedTime(new Date());
        //修改敏感词
        this.updateById(adSensitive);
        //返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    private AdSensitiveMapper adSensitiveMapper;

    @Override
    public ResponseResult<List<String>> selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }
}
